package com.beamfield.controller.api.mb;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.beamfield.common.ApiResult;
import com.beamfield.common.CommonUtils;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.MbBaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.enums.MessageEnum;
import com.beamfield.model.Message;
import com.beamfield.service.manage.sys.MessageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Zephyr
 * @Description: 移动端 - - 消息（待办事项）
 * @Date 2018/10/22
 */
@Controller
@RequestMapping(value = "/mb_api/message")
public class MbMessageController extends MbBaseController {



    final private MessageService messageService;

    @Autowired
    public MbMessageController(MessageService messageService){
        this.messageService = messageService;
    }

    /**
     * 移动需要除外的消息类型
     * @return
     */
    private List<Integer> getMbExceptList(){
        List<Integer> exceptList = new ArrayList<>();

        //发料单消息
        exceptList.add(MessageEnum.MATERIAL_ISSUE.getType());

        //工序审核
        exceptList.add(MessageEnum.PROC_REVIEW.getType());

        //物资需求计划审核
        exceptList.add(MessageEnum.MATERIAL_MONTH_DEMAND_APPR.getType());

        //物资采购计划审核
        exceptList.add(MessageEnum.MATERIAL_PURCHASE_FINANCE_APPR.getType());

        //物资采购计划审核
        exceptList.add(MessageEnum.MATERIAL_PURCHASE_PM_APPR.getType());

        return exceptList;
    }

    /**
     * 获取消息
     * @param request
     * @return
     */
    @RequestMapping(value = "/readMessageData", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult readMessageData(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        String offset = body.getString("offset");
        String limit = body.getString("limit");
        String titleContent = body.getString("titleContent");
        List<Message> attendMachineList = messageService.readMessageData(offset,limit,beamId,userId,titleContent, getMbExceptList());
        long total = messageService.countMessageData(beamId, userId,titleContent, getMbExceptList());
        PageResult pageResult = new PageResult(total, CommonUtils.parseLong(offset, 0L), attendMachineList);
        return new ApiResult(pageResult);
    }

    /**
     * 根据id获取消息
     * @param request
     * @return
     */
    @RequestMapping(value = "/readOneMessage", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult readOneMessage(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        String id = body.getString("id");
        if(StringUtils.isEmpty(id)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Message message = messageService.readOneMessage(id);
        return new ApiResult(message);
    }

    /**
     * 获取未读消息数量
     * @param request
     * @return
     */
    @RequestMapping(value = "/countUnreadMessages",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult countUnreadMessages(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        long total = messageService.countUnreadMessages(beamId, userId, getMbExceptList());
        return new ApiResult(HttpResultEnum.SUCCESS.getCode(), HttpResultEnum.SUCCESS.getMessage(), total);
    }

    /**
     * 获取待办事项数量
     * @param request
     * @return
     */
    @RequestMapping(value = "/countUpcoming", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult countUpcoming(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        Long userId = super.getUserId(request);
        return messageService.countUpcoming(beamId, userId, getMbExceptList());
    }

    /**
     * 更新消息状态
     * @param request
     * @return
     */
    @RequestMapping(value = "/updateMessageStatus",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updateMessageStatus(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        String id = body.getString("id");
        String status = body.getString("status");
        if(StringUtils.isEmpty(id) || StringUtils.isEmpty(status)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        Long userId = super.getUserId(request);
        try {
            int result = messageService.updateMessageStatus(id, userId, status);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return new ApiResult(HttpResultEnum.SUCCESS.getCode(), HttpResultEnum.SUCCESS.getMessage());
    }

    /**
     * 根据消息类型获取相应的消息列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/readUpcomingData", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult readUpcomingData(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        String typeName = body.getString("typeName");

        String offset = body.getString("offset");
        String limit = body.getString("limit");
        Long userId = super.getUserId(request);
        Long beamId = super.getBeamId(request);
        try {
            PageResult pageResult = messageService.readUpcomingData(typeName, offset, limit, userId, beamId);
            return new ApiResult(pageResult);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
    }

    /**
     * 获取计划详情
     * @return
     */
    @RequestMapping(value = "/readPlanDetail", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult readPlanDetail(HttpServletRequest request){
        JSONObject body = super.getBody(request);
        if(body == null){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        String code = body.getString("code");
        String type = body.getString("type");
        String offset = body.getString("offset");
        String limit = body.getString("limit");
        if(StringUtils.isEmpty(code) || StringUtils.isEmpty(type)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }

        try{
            PageResult pageResult = messageService.readPlanDetail(code, type, offset, limit);
            return new ApiResult(pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(), HttpResultEnum.EXCEPTION.getMessage());
        }

    }
}
